<?php

namespace App\Http\Controllers;

use App\Http\Requests\BlogRequest;
use App\Models\Blog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BlogController extends Controller
{
    /**
     * 排除show
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth')->except('show');
    }
    /**
     * 添加博客页面
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('blog.create');
    }

    /**
     * 执行博客的添加
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(BlogRequest $request)
    {

        //获取用户id
        $request->offsetSet('user_id',auth()->id());
        //添加数据
        $res = Blog::create($request->except('_token'));
        if($res){
            return  back()->with(['success'=>'添加数据成功']);
        }else{
            return back()->withErrors('添加失败')->withInput();
        }
    }

    /**
     * 显示一条博客数据
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $blog = Blog::with(['comments' => function ($query) {
                        $query->orderBy('updated_at', 'desc');
                    },
                      'comments.user'
                    ])
            ->where('id',$id)
            ->first();
        // dd($blog);
        $blog->timestamps = false;
        $blog->increment('view');
        $blog->timestamps = true;
        return view('blog.show',['blog'=>$blog]);
    }

    /**
     * 编辑页面
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Blog $blog)
    {
        return view('blog.edit',['blog'=>$blog]);
    }

    /**
     * 执行更新
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(BlogRequest $request, Blog $blog)
    {
        $blog->fill($request->except(['_method','_token']));
        $blog->save();
        if($blog){
            return back()->with(['success'=>'更新成功']);
        }else{
            return back()->withErrors('更新失败');
        }

    }

    /**
     * 删除博客
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Blog $blog)
    {
        //开启事务
        DB::beginTransaction();
        try{
            //删除博客
            $blog->delete();

            //删除博客的评论
            // $blog->comments()->delete();
            //使用事件来删除评论  /app/Observers/BlogObserver.php 中的deleted()方法

            //提交事务
            DB::commit();

            return response()->api('删除成功');
        }catch (\Exception $e){
            //事务回滚
            DB::rollBack();
            return response()->api('删除失败',400);
        }

    }

    /**
     * 改变博客状态  发布与不发布
     * @param int $id
     */
    public function status(Blog $blog){
        $blog->timestamps = false;
        $blog->status = $blog->status == 1 ? 0 : 1;
        $res = $blog->save();
        if($res){
            $msg = $blog->status == 1 ? '发布成功' : '已取消发布';
            return response()->api($msg);
        }else{
            return response()->api('操作失败',400);
        }

    }
}
